// pages/classify/classify.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabActive: 0,
    tabList: [],
    childrenList: [],
    tabCartTotal: '',
    itemId: '',
  },

  tabChange(event){
    let _this = this;
    let index = event.detail.index;

    _this.setData({ tabActive: event.detail.index });

    _this.selectComponent('#goodlist').getGoodList(false,_this.data.tabList[index].id);
  },

  //二级菜单点击
  classifyItem(event){
    let id = event.currentTarget.dataset.id;
    let name = event.currentTarget.dataset.name;
    wx.navigateTo({
      url: '/pages/classify/classify-children/classify-children?id=' + id + '&name=' + name
    })
  },

  //获取tab
  getTabs(){
    let _this = this;
    let user = app.getUser();
    app.requestUrl({
      method: 'GET',
      url: '/wxapi/category',
      data: {
        parentid: 0,
        type:4,
        centreId: app.globalData.loginCentreId
      }
    }).then((res)=>{
      let list = _this.makeTree(res.data.data);
      let first = {
        id:'',
        name:'全部',
      };
      list.unshift(first);
      _this.setData({
        tabList: list,
        childrenList: list[_this.data.tabActive].categoryList
      });

      _this.selectComponent('#goodlist').getGoodList(false,_this.data.tabList[_this.data.tabActive].id);

    })
  },
  //处理tab数据
  makeTree(data){
    let tree = {};
    data.forEach(item => {
      if (item.enabled &&  (item.status === 1)) {
        tree[item.id] = item;
      }
    });

    data.forEach(item => {
      if (item.enabled &&  (item.status === 1) && (item.parentid > 0)) {
        let parent = tree[item.parentid];

        if (parent) {
          if (!parent.children) {
            parent.children = [];
          }
          parent.children.push(item);
        }
      }
    });

    return data.filter(item => (item.parentid === 0) && ((item.enabled &&  (item.status === 1))));
  },

  /**
   * 页面滚动
   */
  onPageScroll (e) {
    this.selectComponent('#goodlist').cOnPageScroll(e);
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let _this = this;
    wx.showNavigationBarLoading() //在标题栏中显示加载
    setTimeout(() => {
      let tt = { detail: { index: this.data.tabActive } }
      console.log(tt);
      this.tabChange(tt)
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500)
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.selectComponent('#goodlist').bottomList()
  },

  //监听app全局变量变化回调
  watchBack: function (value){
    this.setData({
      tabCartTotal: value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (event) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let _this = this;
    this.getTabs();
    _this.setData({
      tabCartTotal: wx.getStorageSync('cartTotal')
    })
    app.watch(this.watchBack);//监听app全局变量
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let index = this.data.tabActive;
    let ii = "#goodlist" + index;
    if(this.selectComponent(ii))
      this.selectComponent(ii).bottomList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 跳转搜索列表
  toSearch:function(){
    wx.navigateTo({
      url: '/pages/index/search/search?type=0'
    })
  },
  // 跳转所有分类
  jumpClassifyAll:function(){
    wx.navigateTo({
      url: '/pages/classify/classify-all/classify-all'
    })
  },
  // 跳转至分类内商品列表
  jumpGoodList:function(e){
    wx.navigateTo({
      url: '/pages/classify/goodsList/goodsList?status='+e.currentTarget.dataset.status
    })
  }
})
